{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Table of Contents\n",
    "* [Intro](#Intro)\n",
    "* [Load and Plot Image](#Load-and-Plot-Image)\n",
    "\t* [Plot Color Channels](#Plot-Color-Channels)\n",
    "* [Image from array](#Image-from-array)\n",
    "\t* [Morphological Operations](#Morphological-Operations)\n",
    "* [Convolution Filters](#Convolution-Filters)\n",
    "\t* [Gaussian Convolution](#Gaussian-Convolution)\n",
    "* [Images Normalization](#Images-Normalization)\n",
    "\t* [Mean/Deviation of Images](#Mean/Deviation-of-Images)\n",
    "\t* [Normalization](#Normalization)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Intro"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notebook that explores the basics of image processing in Python, like image loading, representation and transformations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-10-10T09:19:33.920738",
     "start_time": "2017-10-10T09:19:29.264471Z"
    }
   },
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import os\n",
    "import sys\n",
    "sys.path.append(os.path.join(os.pardir))\n",
    "\n",
    "from utils import image_processing\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "sns.set_style(\"dark\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load and Plot Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-03T14:13:36.006532",
     "start_time": "2017-07-03T14:13:36.002532"
    }
   },
   "outputs": [],
   "source": [
    "img_path = os.path.join(os.path.pardir, 'resources', 'mona_lisa.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-03T14:13:37.597623",
     "start_time": "2017-07-03T14:13:36.939585"
    }
   },
   "outputs": [],
   "source": [
    "# Load with open-cv\n",
    "import cv2\n",
    "# open-cv represents RGB images as BGR (reverse order)\n",
    "img = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB))\n",
    "sns.plt.imshow(img)\n",
    "sns.plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-03T14:17:24.158582",
     "start_time": "2017-07-03T14:17:24.005573"
    }
   },
   "outputs": [],
   "source": [
    "# Load with scikit\n",
    "from skimage import io\n",
    "img = io.imread(img_path)\n",
    "sns.plt.imshow(img)\n",
    "sns.plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-05-25T14:35:36.968293",
     "start_time": "2017-05-25T14:35:36.811284"
    }
   },
   "outputs": [],
   "source": [
    "# Load with PIL\n",
    "from PIL import Image\n",
    "with Image.open(img_path) as img:\n",
    "    sns.plt.imshow(img.convert(mode='RGB'))\n",
    "    sns.plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## Plot Color Channels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-03T14:20:59.746912",
     "start_time": "2017-07-03T14:20:59.596904"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# red color channel\n",
    "sns.plt.imshow(img[:,:,0])\n",
    "sns.plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-03T14:21:22.604220",
     "start_time": "2017-07-03T14:21:22.407209"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# red color channel in grey color map \n",
    "sns.plt.imshow(img[:,:,0], cmap='gray')\n",
    "sns.plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# Image from array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T10:11:55.596834",
     "start_time": "2017-07-04T10:11:55.590834"
    },
    "collapsed": true,
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# dummy list of strings representing our image\n",
    "a = [\"0000000000\",\n",
    "     \"0111111100\",\n",
    "     \"0000111100\",\n",
    "     \"0000111100\",\n",
    "     \"0001111100\",\n",
    "     \"0000111100\",\n",
    "     \"0001100000\",\n",
    "     \"0000000000\",\n",
    "     \"0000000000\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T10:11:56.257872",
     "start_time": "2017-07-04T10:11:56.246871"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# build numpy array of 0s and 1s from previous list\n",
    "a = np.array([list(map(int, s)) for s in a], dtype=np.float32)\n",
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T10:12:51.042005",
     "start_time": "2017-07-04T10:12:50.920998"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# plot image\n",
    "plt.imshow(a, cmap='gray', interpolation='none', vmin=0, vmax=1)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "## Morphological Operations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T10:17:38.542449",
     "start_time": "2017-07-04T10:17:38.395441"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from skimage import morphology\n",
    "b = np.array([[1,1,1],\n",
    "            [1,1,1],\n",
    "            [1,1,1]])\n",
    "res = morphology.binary_dilation(a, b).astype(np.uint8)\n",
    "#res = morphology.binary_erosion(res, b).astype(np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T10:17:40.823580",
     "start_time": "2017-07-04T10:17:40.706573"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "plt.imshow(res, cmap='gray', interpolation='none', vmin=0, vmax=1)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-05-25T14:36:53.276657",
     "start_time": "2017-05-25T14:36:53.268657"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "b = np.array([[0,0,0,0],\n",
    "            [0,1,1,0],\n",
    "            [0,0,0,0]])\n",
    "s = np.array([[1,0],\n",
    "              [1,1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-05-25T14:36:55.486784",
     "start_time": "2017-05-25T14:36:55.479783"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "res = morphology.binary_dilation(b, s).astype(np.uint8)\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-05-25T14:36:56.304831",
     "start_time": "2017-05-25T14:36:56.186824"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "plt.imshow(res, cmap='gray', interpolation='none', vmin=0, vmax=1)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# Convolution Filters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "http://setosa.io/ev/image-kernels/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T10:13:17.687529",
     "start_time": "2017-07-04T10:13:17.254505"
    },
    "collapsed": true,
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from scipy import ndimage\n",
    "from skimage import data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T10:13:18.235561",
     "start_time": "2017-07-04T10:13:18.041550"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# load initial ref image\n",
    "image = data.coins()\n",
    "plt.imshow(image, cmap='gray', interpolation='none')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T10:13:33.304423",
     "start_time": "2017-07-04T10:13:33.290422"
    },
    "collapsed": true,
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# convolve image with custom kernel\n",
    "k = np.array([[1/16,1/8,1/16],\n",
    "            [1/18,1,1/8],\n",
    "            [1/16,1/8,1/16]])\n",
    "n_image = ndimage.convolve(image, k, mode='constant', cval=0.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T10:13:34.154471",
     "start_time": "2017-07-04T10:13:33.968461"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# plot convolved image\n",
    "plt.imshow(n_image, cmap='gray', interpolation='none')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "## Gaussian Convolution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "Try convolving an image using a Gaussian kernel."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T10:38:28.643951",
     "start_time": "2017-07-04T10:38:28.632951"
    },
    "collapsed": true,
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# gaussian distribution formula\n",
    "def gaussian(x, mu, sig):\n",
    "    return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T11:17:30.885920",
     "start_time": "2017-07-04T11:17:30.743912"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# plot 1D gaussian curve\n",
    "x = np.linspace(-3.0, 3.0, 6)\n",
    "z = gaussian(x, 0, 1)\n",
    "plt.plot(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T11:17:31.505955",
     "start_time": "2017-07-04T11:17:31.380948"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# compute 2D gaussian\n",
    "z_2d = np.matmul(z.reshape(-1, 1), z.reshape(1, -1))\n",
    "plt.imshow(z_2d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T11:17:34.101104",
     "start_time": "2017-07-04T11:17:33.898092"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# load initial ref image\n",
    "image = data.camera()\n",
    "plt.imshow(image, cmap='gray')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T11:17:34.865147",
     "start_time": "2017-07-04T11:17:34.643135"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# convolve\n",
    "n_image = ndimage.convolve(image, z_2d)\n",
    "plt.imshow(n_image, cmap='gray', interpolation='none')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Images Normalization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using [CelebA Dataset](http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html)\n",
    "\n",
    "Reference Course: [creative-applications-of-deep-learning-with-tensorflow](https://www.kadenze.com/courses/creative-applications-of-deep-learning-with-tensorflow-iv/sessions/introduction-to-tensorflow)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-30T10:49:57.473995",
     "start_time": "2017-07-30T10:49:55.917906"
    }
   },
   "outputs": [],
   "source": [
    "# load all imgs filepaths for the celeba database\n",
    "dir_path = os.path.join(os.pardir, 'resources', 'img_align_celeba')\n",
    "imgs_filepath = [os.path.join(dir_path, img_name) for img_name in os.listdir(dir_path)]\n",
    "print(len(imgs_filepath))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-16T11:26:24.499209",
     "start_time": "2017-08-16T11:26:02.577955"
    }
   },
   "outputs": [],
   "source": [
    "# load subset of images\n",
    "imgs = image_processing.load_data(imgs_filepath[:100])\n",
    "imgs.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-03T15:30:33.601643",
     "start_time": "2017-07-03T15:30:33.597643"
    }
   },
   "source": [
    "## Mean/Deviation of Images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-08-16T11:26:50.591701",
     "start_time": "2017-08-16T11:26:49.704650"
    }
   },
   "outputs": [],
   "source": [
    "# compute mean images (across list of images, so axis=0)\n",
    "mean_img = np.mean(imgs, axis=0)\n",
    "# plot image (convert to int values)\n",
    "plt.imshow(mean_img.astype(np.uint8))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T09:56:18.749249",
     "start_time": "2017-07-04T09:56:18.568239"
    }
   },
   "outputs": [],
   "source": [
    "# compute std images (across list of images, so axis=0)\n",
    "std_img = np.std(imgs, axis=0)\n",
    "# plot image (convert to int values)\n",
    "plt.imshow(std_img.astype(np.uint8))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T10:08:40.251661",
     "start_time": "2017-07-04T10:08:40.086652"
    }
   },
   "outputs": [],
   "source": [
    "plt.imshow(np.mean(std_img, axis=2).astype(np.uint8))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Normalization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T09:51:55.700204",
     "start_time": "2017-07-04T09:51:55.696204"
    }
   },
   "outputs": [],
   "source": [
    "# flatten imgs to single vector\n",
    "flattened_imgs = imgs.ravel()\n",
    "print(flattened_imgs.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T09:55:54.419858",
     "start_time": "2017-07-04T09:55:53.619812"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# plot flattened images\n",
    "(_, _, _) = plt.hist(flattened_imgs, bins=255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T09:56:50.028039",
     "start_time": "2017-07-04T09:56:49.466006"
    }
   },
   "outputs": [],
   "source": [
    "# plot flattened mean image\n",
    "(_, _, _) = plt.hist(mean_img.ravel(), bins=255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T10:00:04.371154",
     "start_time": "2017-07-04T10:00:03.929129"
    }
   },
   "outputs": [],
   "source": [
    "bins = 20\n",
    "fig, axs = plt.subplots(1, 3, figsize=(12, 6), sharey=True, sharex=True)\n",
    "axs[0].hist((imgs[0]).ravel(), bins)\n",
    "axs[0].set_title('img distribution')\n",
    "axs[1].hist((mean_img).ravel(), bins)\n",
    "axs[1].set_title('mean distribution')\n",
    "axs[2].hist((imgs[0] - mean_img).ravel(), bins)\n",
    "axs[2].set_title('(img - mean) distribution')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-07-04T10:05:39.688333",
     "start_time": "2017-07-04T10:05:39.390316"
    }
   },
   "outputs": [],
   "source": [
    "# normalized image (remove mean and divide by std)\n",
    "(_, _, _) = plt.hist(((imgs[0] - mean_img)/std_img).ravel(), bins=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:neural-networks]",
   "language": "python",
   "name": "conda-env-neural-networks-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  },
  "toc": {
   "nav_menu": {
    "height": "104px",
    "width": "252px"
   },
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
